package lichunUtil;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * A wrapper. 
 * Encapsulate a String key to a double value.
 * @author lichunyang
 */
public class WrapStringValue {
	public String key;
	public double value;
	public WrapStringValue() {
		key =null;
		value =0;
	}
	public WrapStringValue(String k, double v) {
		key =k;
		value =v;
	}
	/**
	 * A comparator of key just using the string comparator.
	 * @return
	 */
	static public Comparator<WrapStringValue> getCompKey() {
		return WSV_CompKey.get();
	}
	/**
	 * A comparator of key just using the string comparator reverse.
	 * @return
	 */
	static public Comparator<WrapStringValue> getCompKey_re() {
		return WSV_CompKey_re.get();
	}
	/**
	 * A comparator that makes the high value appear first.
	 * @return 
	 */
	static public Comparator<WrapStringValue> getCompValue_dec() {
		return WSV_CompValue_dec.get();
	}
	/**
	 * A comparator that makes the low value appear first.
	 * @return 
	 */
	static public Comparator<WrapStringValue> getCompValue_inc() {
		return WSV_CompValue_inc.get();
	}
	static public Map<String, Double> tran2map (Iterable<WrapStringValue> list) {
		Map<String, Double> res = new HashMap<String, Double>();
		for (WrapStringValue wsv : list) {
			res.put(wsv.key, wsv.value);
		}
		return res;
	}
	static public Map<String, Double> tran2map (WrapStringValue[] list) {
		Map<String, Double> res = new HashMap<String, Double>();
		for (WrapStringValue wsv : list) {
			res.put(wsv.key, wsv.value);
		}
		return res;
	}
	static public ArrayList<WrapStringValue> tran2list (Map<? extends Object, ? extends Number> map) {
		ArrayList<WrapStringValue> res = new ArrayList<WrapStringValue>();
		for (Entry<? extends Object, ? extends Number> e : map.entrySet()) {
			res.add(new WrapStringValue(e.getKey().toString(), e.getValue().doubleValue()));
		}
		return res;
	}
	static public WrapStringValue[] tran2array (Map<? extends Object, ? extends Number> map) {
		WrapStringValue[] res = new WrapStringValue[map.size()];
		int pos=0;
		for (Entry<? extends Object, ? extends Number> e : map.entrySet()) {
			res[pos++]=new WrapStringValue(e.getKey().toString(), e.getValue().doubleValue());
		}
		return res;
	}
	static public ArrayList<WrapStringValue> tran2list_string (Map<? extends Object, String> map) {
		ArrayList<WrapStringValue> res = new ArrayList<WrapStringValue>();
		for (Entry<? extends Object, String> e : map.entrySet()) {
			res.add(new WrapStringValue(e.getKey().toString(), Double.parseDouble(e.getValue())));
		}
		return res;
	}
	static public WrapStringValue[] tran2array_string (Map<? extends Object, String> map) {
		WrapStringValue[] res = new WrapStringValue[map.size()];
		int pos=0;
		for (Entry<? extends Object, String> e : map.entrySet()) {
			res[pos++]=new WrapStringValue(e.getKey().toString(), Double.parseDouble(e.getValue()));
		}
		return res;
	}
	
	public String toString() {
		StringBuilder res = new StringBuilder();
		res.append(key);
		res.append("\t");
		res.append(value);
		return res.toString();
	}
	public int hashCode() {
		return toString().hashCode();
	}
	public boolean equals (Object o) {
		if (o instanceof WrapStringValue)
			return toString().equals(o.toString());
		return false;
	}
}

class WSV_CompKey implements Comparator<WrapStringValue> {
	static private WSV_CompKey ins = null;
	static public WSV_CompKey get() {
		if (ins==null)
			ins = new WSV_CompKey();
		return ins;
	}
	
	public int compare(WrapStringValue arg0, WrapStringValue arg1) {
		return arg0.key.compareTo(arg1.key);
	}
	
}

class WSV_CompKey_re implements Comparator<WrapStringValue> {
	static private WSV_CompKey_re ins = null;
	static public WSV_CompKey_re get() {
		if (ins==null)
			ins = new WSV_CompKey_re();
		return ins;
	}
	
	public int compare(WrapStringValue arg0, WrapStringValue arg1) {
		return arg1.key.compareTo(arg0.key);
	}
	
}

class WSV_CompValue_dec implements Comparator<WrapStringValue> {
	static private WSV_CompValue_dec ins = null;
	static public WSV_CompValue_dec get() {
		if (ins==null)
			ins = new WSV_CompValue_dec();
		return ins;
	}
	public int compare(WrapStringValue arg0, WrapStringValue arg1) {
		if (arg0==null) return 1;
		if (arg1==null) return -1;
		if (arg1.value>arg0.value) return 1;
		if (arg1.value<arg0.value) return -1;
		return 0;
	
	}
}


class WSV_CompValue_inc implements Comparator<WrapStringValue> {
	static private WSV_CompValue_inc ins = null;
	static public WSV_CompValue_inc get() {
		if (ins==null)
			ins = new WSV_CompValue_inc();
		return ins;
	}
	public int compare(WrapStringValue arg0, WrapStringValue arg1) {
		if (arg0==null) return 1;
		if (arg1==null) return -1;
		if (arg1.value>arg0.value) return -1;
		if (arg1.value<arg0.value) return 1;
		return 0;
	
	}
}